package top.zlcode.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.zlcode.api.file.vo.FileDeleteVO;
import top.zlcode.api.file.vo.FileUploadVO;
import top.zlcode.file.domain.dto.FileOperationDTO;
import top.zlcode.common.core.domain.R;
import top.zlcode.file.service.IFileService;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private IFileService fileService;

    /**
     * 上传文件接口
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    R<FileUploadVO> upload(
            @RequestPart("file") MultipartFile file,
            @RequestPart("bucketName") String bucketName,
            @RequestPart("endpoint") String endpoint) {
        return fileService.upload(FileOperationDTO.builder().file(file).bucketName(bucketName).endpoint(endpoint).build());
    }

    /**
     * 通过完整文件名删除远程文件
     */
    @PostMapping("/delete")
    R<FileDeleteVO> delete(@RequestParam("wholeFileName") String wholeFileName) {
        return fileService.delete(wholeFileName);
    }

    /**
     * 通过完整文件名获取远程文件地址
     */
    @GetMapping("/getRemoteUrl")
    public R<String> getRemoteUrl(@RequestParam("wholeFileName") String wholeFileName) {
        return fileService.getRemoteUrl(wholeFileName);
    }

}
